/*=========================================================================
Elite College as Engines of Upward Mobility: Evidence from Colombia's Ser Pilo Paga
Authors: Juliana Londoño-Vélez, Catherine Rodriguez, Fabio Sánchez
and Luis Esteban Álvarez-Arango

Creation date: June 6th, 2025
--------------------------------------------------------------------------
Tables B1 and B2
=========================================================================*/

glo cond_saber11	"eligible_sisben==1"
glo cond_sisben		"eligible_saber11==1"

use "${data}/data_RD", clear
keep if icfes_per==20142

********************************************************************************
*** ESTIMACIONES BW FIJO
********************************************************************************

foreach o in distancia_0 distancia_1 distancia_2 distancia_3 distancia_4 distancia_5 distancia_6{
	foreach w in hq lq pri pub hq_pri hq_pub lq_pri lq_pub{
		gen `o'_`w'=`o' if `w'==1
	}
}

fasterxtile wage0_8_ptile=wage0_8, nq(100)
gen wage0_8_top1=(wage0_8_ptile==100)
gen wage0_8_top25=(wage0_8_ptile>75 & wage0_8_ptile!=.)

fasterxtile wage0_9_ptile=wage0_9, nq(100)
gen wage0_9_top1=(wage0_9_ptile==100)
gen wage0_9_top25=(wage0_9_ptile>75 & wage0_9_ptile!=.)


global outcomes access_0 access_6 access_0_hq access_0_lq access_0_pri access_0_pub access_0_hq_pri access_0_hq_pub access_0_lq_pri access_0_lq_pub access_6_hq access_6_lq access_6_pri access_6_pub access_6_hq_pri access_6_hq_pub access_6_lq_pri access_6_lq_pub access_pro_0 access_pro_6 access_tyt_0 access_tyt_6 graduation_exam graduation_exam_hq graduation_exam_lq graduation_exam_pri graduation_exam_pub graduation_exam_hq_pri graduation_exam_hq_pub graduation_exam_lq_pri graduation_exam_lq_pub graduation_exam_pro graduation_exam_pro_hq graduation_exam_pro_lq graduation_exam_pro_pri graduation_exam_pro_pub graduation_exam_pro_hq_pri graduation_exam_pro_hq_pub graduation_exam_pro_lq_pri graduation_exam_pro_lq_pub score_saberpro_5 score_saberpro_7 wage0_8_ptile wage0_8_top1 wage0_8_top25 work_9 mw_9 wage0_9 ln_wage_9 wage0_9_ptile wage0_9_top1 wage0_9_top25

local NO: word count of $outcomes
local NO=`NO'-1
matrix bw=J(`NO',72,.)

local i=1
foreach o of global outcomes{
	foreach r in saber11 sisben{
	if "`r'"=="saber11" local j=0
	if "`r'"=="sisben" 	local j=36
	
	cap rdrobust `o' running_`r' if ${cond_`r'}, masspoints(off)
	if _rc==0{
	local bw_11 `e(h_r)'
	matrix bw[`i',`=`j'+1']=e(tau_cl)
	matrix bw[`i',`=`j'+2']=e(tau_bc)
	matrix bw[`i',`=`j'+3']=e(se_tau_cl)
	matrix bw[`i',`=`j'+4']=e(se_tau_rb)
	matrix bw[`i',`=`j'+5']=e(ci_r_cl)
	matrix bw[`i',`=`j'+6']=e(ci_l_cl)
	
	local f = `=`j'+6'
	foreach p of numlist 0.75 1.25 1.5 1.75 2{
		local bw=`bw_11'*`p'
		rdrobust `o' running_`r' if ${cond_`r'}, masspoints(off) h(`bw')
		matrix bw[`i',`=`f'+1']=e(tau_cl)
		matrix bw[`i',`=`f'+2']=e(tau_bc)
		matrix bw[`i',`=`f'+3']=e(se_tau_cl)
		matrix bw[`i',`=`f'+4']=e(se_tau_rb)
		matrix bw[`i',`=`f'+5']=e(ci_r_cl)
		matrix bw[`i',`=`f'+6']=e(ci_l_cl)
		local f = `=`f'+6'
	}
	}
	else {
		dis as error "`o' `r' could not be run"
	}

	
	}	

	local i=1+`i'
}

***********************************
** Construcción Tablas de resultados
***********************************	

clear
svmat bw
gen nombre=""
local NO: word count of $outcomes
local NO=`NO'-1
forvalues i=1/`NO'{
	local nn: word `i' of ${outcomes}
	replace nombre="`nn'" in `i'
}
order nombre

rename 	(bw1 bw2 bw3 bw4 bw5 bw6 bw7 bw8 bw9 bw10 bw11 bw12 bw13 bw14 bw15 bw16 bw17 bw18 bw19 bw20 bw21 bw22 bw23 bw24 bw25 bw26 bw27 bw28 bw29 bw30 bw31 bw32 bw33 bw34 bw35 bw36) ///
(cc_11100 cr_11100 sec_11100 ser_11100 cs_11100 ci_11100 cc_1175 cr_1175 sec_1175 ser_1175 cs_1175 ci_1175 cc_11125 cr_11125 sec_11125 ser_11125 cs_11125 ci_11125 cc_11150 cr_11150 sec_11150 ser_11150 cs_11150 ci_11150 cc_11175 cr_11175 sec_11175 ser_11175 cs_11175 ci_11175 cc_11200 cr_11200 sec_11200 ser_11200 cs_11200 ci_11200)
rename  (bw37 bw38 bw39 bw40 bw41 bw42 bw43 bw44 bw45 bw46 bw47 bw48 bw49 bw50 bw51 bw52 bw53 bw54 bw55 bw56 bw57 bw58 bw59 bw60 bw61 bw62 bw63 bw64 bw65 bw66 bw67 bw68 bw69 bw70 bw71 bw72) ///
(cc_si100 cr_si100 sec_si100 ser_si100 cs_si100 ci_si100 cc_si75 cr_si75 sec_si75 ser_si75 cs_si75 ci_si75 cc_si125 cr_si125 sec_si125 ser_si125 cs_si125 ci_si125 cc_si150 cr_si150 sec_si150 ser_si150 cs_si150 ci_si150 cc_si175 cr_si175 sec_si175 ser_si175 cs_si175 ci_si175 cc_si200 cr_si200 sec_si200 ser_si200 cs_si200 ci_si200)

save "${outcomes}/Table_B", replace
